package com.pinyoumei.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.BaseBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-08-16.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-16        Administrator             新增Person类
 * 2021-08-16        Administrator             增加sex属性
 */
public class ChangeLoginPswActivity extends BaseActivity implements View.OnClickListener {

    private EditText edittext_old_password;
    private EditText edittext_new_password;
    private EditText edittext_again_password;
    private Button bt_register;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_login);

        InitWidget();
    }

    private void InitWidget() {
        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);

        edittext_old_password = findViewById(R.id.edittext_old_password);
        edittext_new_password = findViewById(R.id.edittext_new_password);
        edittext_again_password = findViewById(R.id.edittext_again_password);


        TextChange textChange = new TextChange();
        edittext_old_password.addTextChangedListener(textChange);
        edittext_new_password.addTextChangedListener(textChange);
        edittext_again_password.addTextChangedListener(textChange);

        bt_register = findViewById(R.id.bt_register);
        bt_register.setOnClickListener(this);
    }


    //EditText的监听器
    class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (edittext_old_password.length() > 0 &&
                    edittext_new_password.length() > 0&&
                    edittext_again_password.length() > 0) {
                bt_register.setBackground(ContextCompat.getDrawable(ChangeLoginPswActivity.this, R.drawable.shanpe_green_5dp));
                bt_register.setEnabled(true);
                bt_register.setClickable(true);
            } else {
                bt_register.setBackground(ContextCompat.getDrawable(ChangeLoginPswActivity.this, R.drawable.shanpe_gray_green_5dp));

                bt_register.setEnabled(false);
                bt_register.setClickable(false);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish:
        		finish();
        		break;

        		case R.id.bt_register:

                    String old_pwd = edittext_old_password.getText().toString().trim();
                    String new_pwd = edittext_new_password.getText().toString().trim();
                    String confirm_pwd = edittext_again_password.getText().toString().trim();

                    if (TextUtils.isEmpty(old_pwd)){
                        ToastUtils.showShort("请输入原密码");
                    }else  if (TextUtils.isEmpty(new_pwd)){
                        ToastUtils.showShort("请输入新密码");
                    }else  if (TextUtils.isEmpty(confirm_pwd)){
                        ToastUtils.showShort("请再次输入新密码");
                    }else{
                        Submit(old_pwd,new_pwd,confirm_pwd);
                    }



        		break;

        	default:
        		break;
        }
    }

    private void Submit(String old_pwd,String new_pwd,String confirm_pwd) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(ChangeLoginPswActivity.this));
        map.put("old_pwd", old_pwd);
        map.put("new_pwd", new_pwd);
        map.put("confirm_pwd", confirm_pwd);

        OkGo.<String>post(Constants.SETTING_LOGIN_PWD)
                .params("user_id", SPUtils.getAppUserId(ChangeLoginPswActivity.this))
                .params("old_pwd", old_pwd)
                .params("new_pwd", new_pwd)
                .params("confirm_pwd", confirm_pwd)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        BaseBean settingBean = gson.fromJson(response.body(), BaseBean.class);

                        if (settingBean.getCode() >= 0) {

                            ToastUtils.showShort(settingBean.getMsg());
                            finish();

                            SPUtils.clearAppUserid(ChangeLoginPswActivity.this);
                            SPUtils.clearUserPhone(ChangeLoginPswActivity.this);
                            finish();
                            appManager.killAllActivity();
                            startActivity(new Intent(ChangeLoginPswActivity.this, LoginActivity.class));

                        } else {
                            ToastUtils.showShort(settingBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }
}
